package com.un.ebs.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.product.domain.ProductPack;
import com.un.ebs.product.mapper.ProductPackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.un.ebs.product.service.ProductPackService;
import com.un.ebs.product.dto.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import org.springframework.web.multipart.MultipartFile;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-11-09
 */
@RestController
@RequestMapping("/productPack")
@Api(value = "product-pack", description = "商品-打包品")
public class ProductPackController {

    @Autowired
    private ProductPackService service;

    @Autowired
    private ProductPackMapper productPackMapper;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody ProductPackAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping(value = "addImage",produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "addImage", notes = "上传图片")
    public WebResult addImage(@RequestParam(value = "file")MultipartFile file, @RequestParam(value = "packId") String packId) {
        return WebUtils.success(service.uploadImage(file, packId));
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody ProductPackUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 ProductPack")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "分页查询所有打包品")
    public WebResult list(@RequestParam String searchText, @RequestParam int pageSize,@RequestParam int pageIndex) {
        Page<ProductPack> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<ProductPack> lq = Wrappers.lambdaQuery();
        lq.eq(ProductPack::getIsDelete, false);
        if(!StringUtils.isNullOrEmpty(searchText)){
            lq.and(s->s.like(ProductPack::getName,searchText).or()
                    .like(ProductPack::getRemark,searchText).or()
                    .like(ProductPack::getCode,searchText)
            );
        }
        return WebUtils.success(productPackMapper.selectPage(page, lq));
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 ProductPack")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @DeleteMapping("deleteImage/{id}")
    @ApiOperation(value = "deleteImage", notes = "删除图片")
    public WebResult deleteImage(@PathVariable("id") String id) {
        service.deleteImage(id);
        return WebUtils.success();
    }

    @DeleteMapping("deleteImageByPackId/{packId}")
    @ApiOperation(value = "deleteImageByPackId", notes = "删除图片")
    public WebResult deleteImageByPackId(@PathVariable("packId") String packId) {
        service.deleteImageByPackId(packId);
        return WebUtils.success();
    }
}